package {
  import flash.utils.ByteArray;
  import org.flixel.*;

  public class TitleScreen extends FlxState {
	  
	// A/B testing variables
	public static const	contextTutorial:Boolean = true; // Math.random() < 0.5 ? true : false;
	
	public static const	toggleShift:Boolean = false;//Math.random() < 0.5 ? true : false;
	
	public static const heatMapMode:Boolean = false;
    
	private var buttons:Array;
	private var titleText:FlxText;
	
	// Music filler
	[Embed(source = "InkMasterMenu.mp3")]
	private var bgMusic:Class;
	private var bgm:FlxSound;
	
	// Splash Image
	[Embed(source = "SplashArt.png")]
	private var bgSplash:Class;
	private var background:FlxSprite;
	
	// Button Backdrop
	[Embed(source = "ButtonBG.png")]
	private var buttonBG:Class;
	
	private var StartButton:FlxIdxBtn;
	private var LevelSelectButton:FlxIdxBtn;
	private var MuteButton:FlxIdxBtn;
	private var musicPlaying:Boolean;
	
	// For Albino Black Sheep Testing
	private var albinoText:FlxText;
	  
	public function TitleScreen() {
		super();
	}
	
	// Set to true to load in development mode
	private const devMode:Boolean = false;
	
	// The development mode level
	[Embed(source = "level22.xml", mimeType = "application/octet-stream")]
	protected var EmbeddedXML:Class;
	
	[Embed(source = "penCursor.png")]
	private var penCursor:Class;
	
	override public function create():void {
		//FlxG.bgColor = 0xffcccccc;
		background = new FlxSprite(0, 0, bgSplash);
		bgm = new FlxSound();
		bgm.loadEmbedded(bgMusic, true, false);
		
		add(background);
		
		//albinoText = new FlxText(230, 270, 200, "permission to be \nhosted by albinoblacksheep.com", true);
		//albinoText.setFormat(null, 6, 0x000000, "center", 0xffffff);
		//add(albinoText);
		
		
		
		
		buttons = new Array();
		
		if ( devMode )
		{
			var ba:ByteArray = new EmbeddedXML() as ByteArray;
			var levelLayout:XML = new XML(ba.readUTFBytes(ba.length));
			var gp:GameProgress = new GameProgress(20);
			var ls:LevelSelect = new LevelSelect(bgm);
			
			levelLayout.ignoreWhitespace = true;
					
			var lvl:Level = new Level( levelLayout, 20, gp, ls, bgm);
			FlxG.switchState(lvl);
		}
		else
		{
			bgm.play();
			var levelSel:LevelSelect = new LevelSelect(bgm);
			
            var lastLevelPlayed:Number = SaveDataManager.furthestLevel();
            StartButton = new FlxIdxBtn(100, 320, "Start Game", levelSel.btnClick, lastLevelPlayed);
			StartButton.loadGraphic(buttonBG, true, false, 160, 80, false);
			StartButton.label.offset.y = -15;
			StartButton.label.offset.x = -40;
			StartButton.label.setFormat(null, 14, 0xffffff, "center", 0);
			buttons.push(StartButton);
			add(buttons[0]);
			LevelSelectButton = new FlxIdxBtn(100, 390, "Level Select", FlxG.switchState, levelSel);
			LevelSelectButton.loadGraphic(buttonBG, true, false, 160, 80, false);
			LevelSelectButton.label.offset.y = -15;
			LevelSelectButton.label.offset.x = -40;
			LevelSelectButton.label.setFormat(null, 14, 0xffffff, "center", 0);
			buttons.push(LevelSelectButton);
			add(buttons[1]);
			MuteButton = new FlxIdxBtn(100, 460, "Toggle Music", muteSound);
			MuteButton.loadGraphic(buttonBG, true, false, 160, 80, false);
			MuteButton.label.offset.y = -15;
			MuteButton.label.offset.x = -40;
			MuteButton.label.setFormat(null, 14, 0xffffff, "center", 0);
			buttons.push(MuteButton);
			add(buttons[2]);
			FlxG.mouse.load(penCursor, 1, 0, 0);
			FlxG.mouse.show();
			musicPlaying = true;
		}
	}
	
	public function muteSound(param:FlxObject): void
	{
		if ( musicPlaying )
		{
			bgm.pause();
			musicPlaying = false;
		}
		else
		{
			bgm.play();
			musicPlaying = true;
		}
	}
  }
}